package net.oschina.spider;

import net.oschina.http.Http;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringEscapeUtils;
import org.json.JSONArray;

/**
 * Created by rex on 2015/3/10.
 */
public class Fetch {
    private int wait = 5000;

    public Fetch(){
        ;
    }

    private String readFile(String filename) throws IOException {
        String everything = "";
        BufferedReader br = new BufferedReader(new FileReader(filename));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null) {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
            everything = sb.toString();
        } finally {
            br.close();
        }

        return everything;
    }

    private String getToken(){
        String content = "";
        try {
            content = this.readFile("token");
        } catch (IOException e) {
            System.out.println("[W] Read token file faild");
            return "";
        }

        JSONArray token = new JSONArray(content);

        long t = System.currentTimeMillis() / 1000;
        int choose = (int) (t % token.length());

        return (String) token.get(choose);
    }

    public String GetUser(String username){
        String url = "";
        String result = "";

        if (this.getToken().length() > 0){
            url = "https://api.github.com/users/" + username + "?access_token="+this.getToken();
        }else{
            url = "https://api.github.com/users/" + username;
        }

        try {
            Http http = new Http();
            result = http.Get(url);
            Thread.sleep(this.wait);
        } catch (IOException e) {
            System.out.println("[E] IOException in GetUser method");
            e.printStackTrace();
        } catch (InterruptedException e) {
            System.out.println("[E] InterruptedException in GetUser method");
            e.printStackTrace();
        }

        return result;
    }

    public String GetUserEmail(String username){
        String url = "https://github.com/" + username;
        String result = null;
        String email = "";

        this.wait = 1;

        try {
            Http http = new Http();
            result = http.Get(url);
            Thread.sleep(this.wait);
        } catch (IOException e) {
            System.out.println("[E] IOException in GetUserEmail method");
            e.printStackTrace();
        } catch (InterruptedException e) {
            System.out.println("[E] InterruptedException in GetUserEmail method");
            e.printStackTrace();
        }

        this.wait = 5000;

        // regexp
        // "mailto:([\d\D][^"]+)"
        String regEx = "\"mailto:([\\d\\D][^\"]+)\"";
        Pattern pat1 = Pattern.compile(regEx);
        Matcher mat1 = pat1.matcher(result);
        boolean rs1 = mat1.find();
        // if not matched
        if (!rs1) {
            return email;
        }else{
            email = mat1.group(1);
            email = StringEscapeUtils.unescapeHtml(email);
            return email;
        }

    }

    public String GetList(int page, String location, String attach){
        String url = "";
        String result = "";

        this.wait = 10000;

        if (this.getToken().length() > 0){
            url = "https://api.github.com/search/users?q=location:" + location + "&page=" + page + "&per_page=100&access_token=" + this.getToken() + attach;
        }else{
            url = "https://api.github.com/search/users?q=location:" + location + "&page=" + page + "&per_page=100" + attach;
        }

        try {
            Http http = new Http();
            result = http.Get(url);
            Thread.sleep(this.wait);
        } catch (IOException e) {
            System.out.println("[E] IOException in GetList method");
            return "";
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        this.wait = 5000;

        return result;
    }

    public String GetNumber(String location){
        String url = "https://api.github.com/search/users?q=location:" + location +"&access_token=" + this.getToken();
        String result = "";

        try {
            Http http = new Http();
            result = http.Get(url);
        } catch (IOException e) {
            System.out.println("[E] IOException in GetNumber method");
            return "";
        }

        return result;
    }
}
